<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading" v-if="false">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="group" :current-node-key="''"
						:highlight-current="true" :expand-on-click-node="false" :default-expand-all="true"
						:filter-node-method="groupFilterNode" @node-click="groupClick">

						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ data.cat_name }}</span>
								<span class="code" v-if="false">{{ node.name }}</span>
								<span class="do">
									<el-button-group>


									</el-button-group>
								</span>
							</span>
						</template>

					</el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0"
						@click="batch_del"></el-button>
					<el-button type="primary" plain :disabled="selection.length==0">开始</el-button>
					<el-button type="primary" plain :disabled="selection.length==0">结束</el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="活动名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<sc-select-filter :data="state" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
					remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="id" width="150"></el-table-column>
					<el-table-column width="90" label="活动封面" column-key="filterAvatar"
						:filters="[{text: '已上传', value: '1'}, {text: '未上传', value: '0'}]">
						<template #default="scope">
							<el-avatar :src="scope.row.image + '?x-oss-process=image/resize,w_90'"
								size="large"></el-avatar>
						</template>
					</el-table-column>
					<el-table-column label="活动名称" prop="title" width="150"></el-table-column>
					<el-table-column label="活动类型" prop="type" width="80">
						<template #default="scope">
							<template v-if="scope.row.form_component">
								表单类
							</template>
							<template v-else>
								分享类
							</template>
						</template>
					</el-table-column>
					<el-table-column label="任务绑定" prop="tasks" width="150"></el-table-column>
					<el-table-column label="类目绑定" prop="category" width="150"></el-table-column>
					<el-table-column label="来源绑定" prop="referrer" width="150"></el-table-column>
					<el-table-column label="起止时间" prop="created_at" width="170" sortable='custom'>
						<template #default="scope">
							{{scope.row.starts_at}} ~ {{scope.row.ends_at}}
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
									@click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-button text type="primary" size="small"
									@click="table_edit(scope.row, scope.$index)">编辑</el-button>
							</el-button-group>
						</template>
					</el-table-column>

				</scTable>
			</el-main>
		</el-container>
	</el-container>

</template>

<script>
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'activityTask',
		components: {
			scSelectFilter
		},
		data() {
			return {
				state: [{
					title: "活动状态",
					key: "product_on_sale",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "进行中",
							value: 1,
							icon: "el-icon-flag"
						},
						{
							label: "未开始",
							value: 0,
							icon: "el-icon-bottom-left"
						},
						{
							label: "已结束",
							value: 0,
							icon: "el-icon-bottom-left"
						}
					]
				}],
				selectedValues: {
					state: [""],
					type: [""]
				},
				filterData: {},
				dialog: {
					save: false
				},
				sendGiftDialog: {
					save: false
				},
				showGrouploading: false,
				groupFilterText: '',
				group: [],
				apiObj: this.$API.system.activityTask.list,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			stateChange(e) {
				var params = {
					state: e
				}
				this.$refs.table.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode.id || null
					});
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.goods.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//加载树数据
			async getGroup() {
				this.showGrouploading = true;
				var res = await this.$API.system.category.tree.get();
				this.showGrouploading = false;
				var allNode = {
					id: '',
					cat_name: '所有',
					dept_path: ''
				}
				res.data.unshift(allNode);
				this.group = res.data;
			},
			//树过滤
			groupFilterNode(value, data) {
				if (!value) return true;
				return data.cat_name.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
